package com.study.spring.ch05.tools;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Locale;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
import org.springframework.format.number.CurrencyStyleFormatter;
import org.springframework.format.support.DefaultFormattingConversionService;

public class Formatter {

//	@Test
	/**
	 * @param args
	 * 使用内建格式转换器
	 */
	public void testCurrencyFormatter() throws Exception{
		//CurrencyFormatter 实现货币样式的格式化/解析
		CurrencyStyleFormatter cf = new CurrencyStyleFormatter();
		cf.setRoundingMode(RoundingMode.CEILING);//舍入模式（ceilling表示四舍五入）
		cf.setFractionDigits(2);//保留小数点后几位
		//1、将带货币符号的字符串“$123.125”转换为BigDecimal("123.00")
		BigDecimal bd=cf.parse("$19.889", Locale.US);
		System.out.println(bd.toString());
		
		//2、将BigDecimal("123")格式化为字符串“$123.00”展示
		bd=new BigDecimal(123.567);
		System.out.println(cf.print(bd, Locale.CHINA));
//		
		
	}
	
//	@Test
	public void testLocaleContextHolder() throws Exception{
		DefaultFormattingConversionService conversionService = new
				DefaultFormattingConversionService();
				//默认不自动注册任何Formatter
		CurrencyStyleFormatter currencyFormatter = new CurrencyStyleFormatter();
		currencyFormatter.setFractionDigits(2);//保留小数点后几位
		currencyFormatter.setRoundingMode(RoundingMode.CEILING);//舍入模式（ceilling表示四舍五入）
		//注册Formatter SPI实现
		conversionService.addFormatter(currencyFormatter);
		
		//绑定Locale信息到ThreadLocal
		//FormattingConversionService内部自动获取作为Locale信息，如果不设值默认是Locale.getDefault()
		LocaleContextHolder.setLocale(Locale.US);//US
		String s1 = conversionService.convert(new BigDecimal("1234.128"), String.class);
		LocaleContextHolder.setLocale(null);
		LocaleContextHolder.setLocale(Locale.CHINA);//CHINE
		String s2 = conversionService.convert(new BigDecimal("1234.128"), String.class);
		BigDecimal b1 = conversionService.convert("￥1,234.13", BigDecimal.class);
		LocaleContextHolder.setLocale(null);
		System.out.println("s1="+s1+",s2="+s2+",b1="+b1);
	}
	
//	@Test
	/**
	 * 
	 * @param args
	 * @throws Exception
	 * 字段级别的解析/格式化
	 */
	public void testFieldFormatter() throws Exception
	{
		//默认自动注册对@NumberFormat和@DateTimeFormat的支持
		DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
		//准备测试模型对象
		ModelA model = new ModelA();
		model.setTotalCount(10000);
		model.setDiscount(0.51);
		model.setSumMoney(10000.13);
		model.setRegisterDate(new Date(2013-1900, 4, 1));
		model.setOrderDate(new Date(2013-1900, 4, 1, 20, 18, 18));
		//获取类型信息
		TypeDescriptor descriptor =new TypeDescriptor(ModelA.class.getDeclaredField("totalCount"));
		TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
		String s1 = (String)conversionService.convert(model.getTotalCount(), descriptor,stringDescriptor);
		int a = (Integer)conversionService.convert("10,000", stringDescriptor, descriptor);
		System.out.println("s1=="+s1+" , a="+a);

		descriptor = new TypeDescriptor(ModelA.class.getDeclaredField("registerDate"));
		stringDescriptor = TypeDescriptor.valueOf(String.class);
		s1 = (String)conversionService.convert(model.getRegisterDate(), descriptor,stringDescriptor);
		Date d1 = (Date)conversionService.convert("2013-05-01", stringDescriptor, descriptor);
		
		descriptor = new TypeDescriptor(ModelA.class.getDeclaredField("orderDate"));
		String s2 = (String)conversionService.convert(model.getOrderDate(), descriptor,stringDescriptor);
		Date d2 = (Date)conversionService.convert("2013-05-01 20:18:18", stringDescriptor,descriptor);
		System.out.println("s1=="+s1+" , d1="+d1+" , s2="+s2+" , d2="+d2);
	}
	
	
//	public static void main4(String[] args) {
//		DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
//		//默认不自动注册任何Formatter
//		CurrencyFormatter currencyFormatter = new CurrencyFormatter();
//		currencyFormatter.setFractionDigits(2);//保留小数点后几位
//		currencyFormatter.setRoundingMode(RoundingMode.CEILING);//舍入模式（ceilling表示四舍五入）
//		//注册Formatter SPI实现
//		conversionService.addFormatter(currencyFormatter);
//		//绑定Locale信息到ThreadLocal
//		//FormattingConversionService内部自动获取作为Locale信息，如果不设值默认是Locale.getDefault()
////		LocaleContextHolder.setLocale(Locale.US);
//		String s1 = conversionService.convert(new BigDecimal("1234.128"), String.class);
////		LocaleContextHolder.setLocale(null);
////		LocaleContextHolder.setLocale(Locale.CHINA);
//		String s2 = conversionService.convert(new BigDecimal("1234.128"), String.class);
//		BigDecimal b1 = conversionService.convert("￥1,234.13", BigDecimal.class);
////		LocaleContextHolder.setLocale(null);
//		System.out.println("s1="+s1+",  s2="+s2+",  b1="+b1);
//
//		
//	}
//public static void main5(String[] args) throws SecurityException, NoSuchFieldException {
//	//默认自动注册对@NumberFormat和@DateTimeFormat的支持
//	DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
//	//准备测试模型对象
//	ModelA model = new ModelA();
//	model.setTotalCount(10000);
//	model.setDiscount(0.51);
//	model.setSumMoney(10000.13);
//	model.setRegisterDate(new Date(2013-1900, 4, 1));
//	model.setOrderDate(new Date(2013-1900, 4, 1, 20, 18, 18));
//	//获取类型信息
//	/*TypeDescriptor descriptor =new TypeDescriptor(ModelA.class.getDeclaredField("totalCount"));
//	TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
//	String s1 = (String)conversionService.convert(model.getTotalCount(), descriptor,stringDescriptor);
//	int a = (Integer)conversionService.convert("10,000", stringDescriptor, descriptor);
//	System.out.println("s1=="+s1+" , a="+a);*/
//	TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
//	TypeDescriptor descriptor = new TypeDescriptor(ModelA.class.getDeclaredField("registerDate"));
//	String s1 = (String)conversionService.convert(model.getRegisterDate(), descriptor,stringDescriptor);
//	Date d1 = (Date)conversionService.convert("2013-05-01", stringDescriptor, descriptor);
//	
//	
//	
//	descriptor = new TypeDescriptor(ModelA.class.getDeclaredField("orderDate"));
//	String s2 = (String)conversionService.convert(model.getOrderDate(), descriptor,stringDescriptor);
//	Date d2 = (Date)conversionService.convert("2013-05-01 20:18:18", stringDescriptor,descriptor);
//	System.out.println("s1=="+s1+" , d1="+d1+" , s2="+s2+" , d2="+d2);
//
//
//}
	
}

class ModelA{
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public double getDiscount() {
		return discount;
	}
	public void setDiscount(double discount) {
		this.discount = discount;
	}
	public double getSumMoney() {
		return sumMoney;
	}
	public void setSumMoney(double sumMoney) {
		this.sumMoney = sumMoney;
	}
	public Date getRegisterDate() {
		return registerDate;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}
	public Date getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}




	@NumberFormat(style=Style.NUMBER, pattern="#,###")//通用样式
	  private int totalCount;
	  @NumberFormat(style=Style.PERCENT)//百分数样式
	  private double discount;
	  
	
	@NumberFormat(style=Style.CURRENCY)//货币样式
	  private double sumMoney;
	  @DateTimeFormat(iso=ISO.DATE)//ISO.DATE(yyyy-MM-dd)
	  private Date registerDate;
	  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")//指定解析/格式化字段数据的模式，如”yyyy-MM-dd HH:mm:ss”
	  private Date orderDate;
}

